package mybatisTest.reflect;

import mybatisTest.test.some.Cat;
import mybatisTest.test.some.WhiteCat;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.invoker.GetFieldInvoker;
import org.apache.ibatis.reflection.invoker.MethodInvoker;
import org.apache.ibatis.reflection.invoker.SetFieldInvoker;
import org.apache.ibatis.reflection.property.PropertyCopier;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class PropertyCopierTest{
    @Test
    public void copyField(){
        WhiteCat red = new WhiteCat("tom", 2, "red");
        WhiteCat blue = new WhiteCat("bob", 3, "blue");
        PropertyCopier.copyBeanProperties(Cat.class,red,blue);
        System.out.println(blue);
    }
//PropertyTokenizer
    @Test
    public void pa(){
        PropertyTokenizer pt = new PropertyTokenizer("name1[2].chidlran1.iiicc[4]");
        while (pt.hasNext()){
            System.out.println("name:"+pt.getName());
            System.out.println("children:"+pt.getChildren());
            System.out.println("IndexName:"+pt.getIndexedName());
            System.out.println("Index:"+pt.getIndex());
            System.out.println("=======================");
            pt=pt.next();
        }
    }
//getFieldInvoker
    @Test
    public void tt() throws NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        Field color =WhiteCat.class.getField("color");
        WhiteCat wc=new WhiteCat("jjj",4,"black");
        Object o = color.get(wc);
        System.out.println(o);

        GetFieldInvoker getFieldInvoker = new GetFieldInvoker(color);
        Object color2 = getFieldInvoker.invoke(wc, null);//获取wc的color属性值
        System.out.println(color2);//black
    }
    //setFieldInvoker,设置对象的属性值
    @Test
    public  void setFied() throws NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        Field color =WhiteCat.class.getField("color");
        WhiteCat wc=new WhiteCat("jjj",4,"black");

        SetFieldInvoker setFieldInvoker=new SetFieldInvoker(color);
        setFieldInvoker.invoke(wc,new Object[]{"green"});
        System.out.println(wc);
    }
    @Test
    public void methodInvoker() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        WhiteCat wc=new WhiteCat("jjj",4,"black");
        Method say =WhiteCat.class.getMethod("say",String.class);
        MethodInvoker methodInvoker=new MethodInvoker(say);
        methodInvoker.invoke(wc, new Object[]{"this is info"});
    }
    @Test
    public void createObject(){
        DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
        Class type=WhiteCat.class;
        List<Class<?>> constructorArgTypes=new ArrayList<>();
        constructorArgTypes.add(String.class);
        constructorArgTypes.add(int.class);
        constructorArgTypes.add(String.class);
        List<Object> constructorArgs=new ArrayList<>();
        constructorArgs.add("jim");
        constructorArgs.add(2);
        constructorArgs.add("pink");
        Object o = defaultObjectFactory.create(type, constructorArgTypes, constructorArgs);
        System.out.println(o);
    }

    public static void main(String[] args) throws ClassNotFoundException {
        WhiteCat wc=new WhiteCat("jjj",4,"black");

        System.out.println("haha");
    }
}
